package com.hotel.comm.lucene.condition;

import org.apache.lucene.index.Term;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TermQuery;

import java.io.Serializable;

/**
 * lucene搜索条件
 *
 * @author XuSemon
 * @date 2-15-4-24
 */
public class SearchCondition implements Serializable, ICondition {
    /**
     *
     */
    private static final long serialVersionUID = 31324132413456L;
    /**
     * 查询条件中对应的字段名
     */
    private String key;
    /**
     * 查询条件中字段对应值的下限<br/>
     * 如果仅需一个参数，通过此字段传递
     */
    private Object valueLow;
    /**
     * 查询条件中字段对应值的上限
     */
    private Object valueHight;

    public SearchCondition() {
    }

    public SearchCondition(String key, Object valueLow) {
        this(key, valueLow, valueLow);
    }

    public SearchCondition(String key, Object valueLow, Object valueHight) {
        this.key = key;
        this.valueLow = valueLow;
        this.valueHight = valueHight;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public Object getValueLow() {
        return valueLow;
    }

    public void setValueLow(Object valueLow) {
        this.valueLow = valueLow;
    }

    public Object getValueHight() {
        return valueHight;
    }

    public void setValueHight(Object valueHight) {
        this.valueHight = valueHight;
    }

    @Override
    public Query getQuery() {
        if (valueLow instanceof String) {
            return new TermQuery(new Term(key, (String) valueLow));
        } else if (valueLow instanceof Integer) {
            return NumericRangeQuery.newIntRange(key, (Integer) valueLow, (Integer) valueHight, true, true);
        } else if (valueLow instanceof Double) {
            return NumericRangeQuery.newDoubleRange(key, (Double) valueLow, (Double) valueHight, true, true);
        } else if (valueLow instanceof Float) {
            return NumericRangeQuery.newFloatRange(key, (Float) valueLow, (Float) valueHight, true, true);
        } else if (valueLow instanceof Long) {
            return NumericRangeQuery.newLongRange(key, (Long) valueLow, (Long) valueHight, true, true);
        }
        return null;
    }
}
